import pygame
import sys
from game import Game
from player1 import Player1
from player2 import Player2
from player3 import Player3
from player4 import Player4
from wall import Wall
from bullet import Bullet
from portal import Portal
from monster import Monster
from mini_portal1 import Portal_mini1
from mini_portal2 import Portal_mini2
from pygame.locals import *


def game_star():
    pygame.init()
    #
    Game.window = pygame.display.set_mode((Game.W, Game.H), FULLSCREEN | HWSURFACE, 32)
    pygame.display.set_caption("游戏零零")

    Game.bg = pygame.image.load("img/背景(1700x900)22.png")
    Background_img_rect = Game.bg.get_rect()

    Game.music = pygame.mixer.Sound("wav/游戏界面.mp3")
    Game.music.play(-1)

    Game.zixinag_img = pygame.image.load("img/纸箱复制.png")

    FPS = 60
    clock = pygame.time.Clock()
    Game.player1 = Player1(0, 750, 4)
    Game.playerGroup = pygame.sprite.Group()
    Game.playerGroup.add(Game.player1)

    Game.player2 = Player2(50, 750, 4)
    Game.playerGroup2 = pygame.sprite.Group()
    Game.playerGroup2.add(Game.player2)

    Game.player3 = Player3(100, 750, 4)
    Game.playerGroup3 = pygame.sprite.Group()
    Game.playerGroup3.add(Game.player3)

    Game.player4 = Player4(150, 750, 4)
    Game.playerGroup4 = pygame.sprite.Group()
    Game.playerGroup4.add(Game.player4)

    Game.player3_x = 100
    Game.player3_y = 750
    Game.wall = Wall()
    Game.portal = Portal(1500, 150)
    Game.monster = Monster(10, 10, 2)
    Game.mini_portal1 = Portal_mini1(0, 300)
    Game.mini_portal2 = Portal_mini2(1550, 0)
    # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    # [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    Game.wall_list = [
        [6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 3, 0, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 6, 5, 0, 0, 0, 1, 1, 3, 3, 3, 1],
        [0, 0, 0, 0, 0, 0, 6, 5, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 4, 6, 0, 6, 2],
        [0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 1, 2, 6, 0, 0, 0, 0, 2, 3, 3, 3, 3, 0, 0, 0, 4, 0, 5, 0, 5],
        [1, 1, 1, 1, 3, 3, 1, 1, 3, 3, 3, 1, 2, 2, 3, 3, 3, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ]

    # 初始化计数器
    count = 0
    Game.fangkuaixiaoshi = False
    # 遍历二维列表，找出所有的5并计数
    for row in Game.wall_list:
        for element in row:
            if element == 5:
                count += 1

    for row in Game.wall_list:
        for element in row:
            if element == 6:
                count += 1

    print(f"二维列表中值为5的元素数量为：{count}")

    Game.monster_list = [
        {"x": 450, "y": 0},
        {"x": 600, "y": 150},
        {"x": 700, "y": 300},
        {"x": 800, "y": 450},
        {"x": 1300, "y": 600},
        {"x": 1200, "y": 750},
    ]
    Game.monster1_star = 250
    Game.monster1_end = 1200

    Game.monster2_star = 450
    Game.monster2_end = 1100

    Game.monster3_star = 700
    Game.monster3_end = 1100

    Game.monster4_star = 1050
    Game.monster4_end = 1400

    Game.monster5_star = 1050
    Game.monster5_end = 1500

    Game.monster6_star = 1050
    Game.monster6_end = 1500

    Game.gem_number = count
    Game.fashe_music = pygame.mixer.Sound("Wav/发射.wav")
    Game.jump_music = pygame.mixer.Sound("wav/跳.wav")

    while True:
        Game.index += 1
        clock.tick(FPS)
        game_get()
        Game.window.blit(Game.bg, Background_img_rect)
        Game.wall.wall_display()
        Game.portal.portal_display()
        portal_hit_player()
        Game.monster.monster_display()
        Game.mini_portal1.portal_display()
        Game.mini_portal2.portal_display()

        Game.mini_portal1.portal_hit_player()
        Game.mini_portal2.portal_hit_player()

        text()
        Game.playerGroup.update(Game.index)
        Game.playerGroup.draw(Game.window)
        Game.player1.confine()
        Game.player1.left_right()
        Game.player1.hit_wall()

        Game.playerGroup2.update(Game.index)
        Game.playerGroup2.draw(Game.window)
        Game.player2.confine()
        Game.player2.left_right()
        Game.player2.hit_wall()

        Game.playerGroup3.update(Game.index)
        Game.playerGroup3.draw(Game.window)
        Game.player3.confine()
        Game.player3.left_right()
        Game.player3.hit_wall()

        Game.playerGroup4.update(Game.index)
        Game.playerGroup4.draw(Game.window)
        Game.player4.confine()
        Game.player4.left_right()
        Game.player4.hit_wall()

        # 子弹绘制
        for bullet in Game.bullet_list:
            bullet.bullet_fire()
            bullet.bullet_display()
            if bullet.Bullet_rect.left <= 0 or bullet.Bullet_rect.left >= Game.W:
                Game.bullet_list.remove(bullet)

        # 重力
        Game.G_speed += Game.G  # 增加垂直速度
        Game.player1.rect.y += Game.G_speed  # 根据垂直速度更新位置

        Game.G_speed2 += Game.G2  # 增加垂直速度
        Game.player2.rect.y += Game.G_speed2  # 根据垂直速度更新位置

        Game.G_speed3 += Game.G3  # 增加垂直速度
        Game.player3.rect.y += Game.G_speed3  # 根据垂直速度更新位置

        Game.G_speed4 += Game.G4  # 增加垂直速度
        Game.player4.rect.y += Game.G_speed4  # 根据垂直速度更新位置

        recur()
        wall_hit_player2()
        wall_hit_player1()

        wall_hit_player3()
        wall_hit_player4()
        monster_hit_players()
        pygame.display.update()


def text():
    guanqia_font = pygame.font.Font("Font/font.ttf", 50)
    shuliang_font = pygame.font.Font("Font/font.ttf", 50)

    level_text = guanqia_font.render("关卡5", True, (0, 0, 0))
    Game.window.blit(level_text, (1450, 20))

    score_text = shuliang_font.render("宝石: " + str(Game.gem_number), True, (100, 25, 254))
    Game.window.blit(score_text, (20, 10))


def game_get():
    for event in pygame.event.get():
        mouse_x, mouse_y = pygame.mouse.get_pos()
        print(mouse_x, mouse_y)
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                Game.music.stop()
                Game().__init__()
                game_star()

            # Game.player1
            if event.key == pygame.K_w and not Game.direction_W:
                Game.jump_music.play()
                print("w")
                Game.G_speed = -Game.speed
                Game.direction_W = True

            if event.key == pygame.K_a:
                print("a")
                Game.direction_A = True
                Game.current_images = Game.player1.images_left

            if event.key == pygame.K_d:
                print("d")
                Game.direction_D = True
                Game.current_images = Game.player1.images_right

            if event.key == pygame.K_s:
                if len(Game.bullet_list) < 1:

                    Game.fashe_music.play()


                    if Game.direction == "left":
                        bullet_right = Bullet(Game.player1.rect.left, Game.player1.rect.top + 30, "left", 10)
                        Game.bullet_list.append(bullet_right)
                    elif Game.direction == "right":
                        bullet_left = Bullet(Game.player1.rect.left, Game.player1.rect.top + 30, "right", 10)
                        Game.bullet_list.append(bullet_left)

            if event.key == pygame.K_i and not Game.direction_W2:
                Game.jump_music.play()
                Game.G_speed2 = -Game.speed2
                Game.direction_W2 = True

            if event.key == pygame.K_j:
                Game.direction_A2 = True
            if event.key == pygame.K_l:
                Game.direction_D2 = True
            if event.key == pygame.K_k:
                Game.fangkuai = True

            if event.key == pygame.K_UP and not Game.direction_W3:
                Game.jump_music.play()
                Game.G_speed3 = -Game.speed3
                Game.direction_W3 = True
            if event.key == pygame.K_LEFT:
                Game.direction_A3 = True
            if event.key == pygame.K_RIGHT:
                Game.direction_D3 = True

            if event.key == pygame.K_KP8 and not Game.direction_W4:
                Game.jump_music.play()
                Game.G_speed4 = -Game.speed4
                Game.direction_W4 = True
            if event.key == pygame.K_KP4:
                Game.direction_A4 = True
            if event.key == pygame.K_KP6:
                Game.direction_D4 = True
            if event.key == pygame.K_KP5:
                Game.fangkuaixiaoshi = True

        if event.type == pygame.KEYUP:
            # Game.player1
            if event.key == pygame.K_a:
                Game.direction_A = False
                print("松开a")
            if event.key == pygame.K_d:
                Game.direction_D = False
                print("松开d")
            if event.key == pygame.K_j:
                Game.direction_A2 = False
            if event.key == pygame.K_l:
                Game.direction_D2 = False
            if event.key == pygame.K_k:
                Game.fangkuai = False

            if event.key == pygame.K_LEFT:
                Game.direction_A3 = False
            if event.key == pygame.K_RIGHT:
                Game.direction_D3 = False

            if event.key == pygame.K_KP4:
                Game.direction_A4 = False
            if event.key == pygame.K_KP6:
                Game.direction_D4 = False
            if event.key == pygame.K_KP5:
                Game.fangkuaixiaoshi = False


def recur():
    if Game.player1.rect.y >= Game.H or Game.player2.rect.y >= Game.H or \
            Game.player3.rect.y >= Game.H or Game.player4.rect.y >= Game.H:
        Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
        Game.sisheng.play()
        Game.music.stop()
        Game().__init__()
        game_star()


def wall_hit_player1():
    for row in range(Game.MAP_HEIGHT):
        for col in range(Game.MAP_WIDTH):
            wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                    Game.BLOCK_SIZE)
            if Game.wall_list[row][col] in (4, 2, 1):
                if Game.player1.rect.colliderect(wall_rect):
                    Game.player1.rect.y = wall_rect.y - Game.player1.rect.height
                    Game.G_speed = 0  # 碰撞后重置垂直速度
                    Game.direction_W = False

            if Game.wall_list[row][col] in (1, 2):
                for bullet in Game.bullet_list:
                    if bullet.Bullet_rect.colliderect(wall_rect):
                        Game.bullet_list.remove(bullet)
                        pengzhuang_music = pygame.mixer.Sound("wav/碰撞.wav")
                        pengzhuang_music.play()

            if Game.wall_list[row][col] == 3:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.player1.rect.colliderect(wall_rect):
                    Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
                    Game.sisheng.play()
                    Game.music.stop()
                    Game().__init__()
                    game_star()

            if Game.wall_list[row][col] == 4:
                for bullet in Game.bullet_list:
                    if bullet.Bullet_rect.colliderect(wall_rect):
                        Game.bullet_list.remove(bullet)
                        Game.wall_list[row][col] = 0
                        pengzhuang_music = pygame.mixer.Sound("wav/碰撞.wav")
                        pengzhuang_music.play()


def wall_hit_player2():
    for row in range(Game.MAP_HEIGHT):
        for col in range(Game.MAP_WIDTH):
            wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                    Game.BLOCK_SIZE)
            if Game.wall_list[row][col] in (1, 2, 4):
                if Game.player2.rect.colliderect(wall_rect):
                    Game.player2.rect.y = wall_rect.y - Game.player2.rect.height
                    Game.G_speed2 = 0  # 碰撞后重置垂直速度
                    Game.direction_W2 = False

            if Game.wall_list[row][col] == 3:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.player2.rect.colliderect(wall_rect):
                    Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
                    Game.sisheng.play()
                    Game.music.stop()
                    Game().__init__()
                    game_star()

            if Game.wall_list[row][col] == 0:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.direction2 == "right":
                    try:
                        if Game.player2.rect.colliderect(wall_rect):
                            if Game.player2.rect.left <= wall_rect.left:
                                Game.window.blit(Game.zixinag_img, (wall_rect.x + 50, wall_rect.y - 30))
                    except:
                        pass
                if Game.direction2 == "left":
                    try:
                        if Game.player2.rect.colliderect(wall_rect):
                            if Game.player2.rect.right >= wall_rect.right:
                                Game.window.blit(Game.zixinag_img, (wall_rect.x - 50, wall_rect.y - 30))
                    except:
                        pass
                if Game.player2.rect.colliderect(wall_rect) and Game.fangkuai and Game.direction2 == "right":
                    try:
                        if Game.wall_list[row + 1][col + 2] == 0:
                            Game.wall_list[row + 1][col + 2] = 4
                        Game.fangkuai = False
                    except:
                        Game.fangkuai = False
                if Game.player2.rect.colliderect(wall_rect) and Game.fangkuai and Game.direction2 == "left":
                    try:
                        if Game.wall_list[row + 1][col - 1] == 0:
                            Game.wall_list[row + 1][col - 1] = 4
                        Game.fangkuai = False
                    except:
                        Game.fangkuai = False


def wall_hit_player3():
    for row in range(Game.MAP_HEIGHT):
        for col in range(Game.MAP_WIDTH):
            wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                    Game.BLOCK_SIZE)

            if Game.wall_list[row][col] in (1, 2, 4):
                if Game.player3.rect.colliderect(wall_rect):
                    Game.player3.rect.y = wall_rect.y - Game.player3.rect.height
                    Game.G_speed3 = 0  # 碰撞后重置垂直速度
                    Game.direction_W3 = False

            if Game.wall_list[row][col] == 3:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.player3.rect.colliderect(wall_rect):
                    Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
                    Game.sisheng.play()
                    Game.player3.rect.x = Game.player3_x
                    Game.player3.rect.y = Game.player3_y
            if Game.wall_list[row][col] == 5:
                if Game.player3.rect.colliderect(wall_rect):
                    Game.wall_list[row][col] = 0
                    Game.gem_number -= 1
                    gem_music = pygame.mixer.Sound("wav/吃宝石.wav")
                    gem_music.play()


def wall_hit_player4():
    for row in range(Game.MAP_HEIGHT):
        for col in range(Game.MAP_WIDTH):
            wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                    Game.BLOCK_SIZE)
            if Game.wall_list[row][col] in (1, 2):

                if Game.player4.rect.colliderect(wall_rect):
                    Game.player4.rect.y = wall_rect.y - Game.player4.rect.height
                    Game.G_speed4 = 0  # 碰撞后重置垂直速度
                    Game.direction_W4 = False

            if Game.wall_list[row][col] == 3:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.player4.rect.colliderect(wall_rect):
                    Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
                    Game.sisheng.play()
                    Game.music.stop()
                    Game().__init__()
                    game_star()

            if Game.wall_list[row][col] == 4:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE)
                if Game.player4.rect.colliderect(wall_rect):
                    Game.player4.rect.y = wall_rect.y - Game.player4.rect.height
                    Game.G_speed4 = 0  # 碰撞后重置垂直速度
                    Game.direction_W4 = False
                    if Game.player4.rect.bottom >= wall_rect.top and Game.fangkuaixiaoshi:
                        Game.wall_list[row][col] = 0
                        Game.fangkuaixiaoshi = False

            if Game.wall_list[row][col] == 6:
                if Game.player4.rect.colliderect(wall_rect):
                    Game.gem_number -= 1
                    Game.wall_list[row][col] = 0
                    gem_music = pygame.mixer.Sound("wav/吃宝石.wav")
                    gem_music.play()


def monster_hit_players():
    for monster in Game.monster_list:
        monster_rect = Game.monster.monster_image.get_rect()
        monster_rect.x = monster["x"]
        monster_rect.y = monster["y"]
        if Game.player1.rect.colliderect(monster_rect) or Game.player2.rect.colliderect(
                monster_rect) or Game.player4.rect.colliderect(monster_rect):
            Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
            Game.sisheng.play()
            pengzhuang_music = pygame.mixer.Sound("wav/碰撞.wav")
            pengzhuang_music.play()
            Game.music.stop()
            Game().__init__()
            game_star()
        if Game.player3.rect.colliderect(monster_rect):
            Game.player3.rect.x = Game.player3_x
            Game.player3.rect.y = Game.player3_y
            Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
            Game.sisheng.play()


def portal_hit_player():
    if Game.portal.Portal_rect.colliderect(Game.player1.rect) and \
            Game.portal.Portal_rect.colliderect(Game.player2.rect) and \
            Game.portal.Portal_rect.colliderect(Game.player3.rect) and \
            Game.portal.Portal_rect.colliderect(Game.player4.rect):

        if Game.gem_number <= 0:
            font = pygame.font.Font("Font/font.ttf", 100)
            score_text = font.render(" 恭喜您成功通过 ", True, (8, 25, 15), (255, 85, 0))
            score_text2 = font.render(" 请点击空格下一关 ", True, (8, 25, 15), (255, 85, 0))
            Game.window.blit(score_text, (250, 250))
            Game.window.blit(score_text2, (250, 375))
            print("已通过")
            Game.shengli = True
            if Game.yinyue:
                shengli = pygame.mixer.Sound("wav/胜利.wav")
                shengli.play()
                Game.music.stop()
                Game.jieshu = False
                Game.yinyue = False
        else:
            font3 = pygame.font.Font("Font/font.ttf", 50)
            score_text = font3.render(f" 还有{Game.gem_number}个宝石没收 ", True, (8, 25, 15), (255, 85, 0))
            Game.window.blit(score_text, (600, 820))
            print(f" 还有{Game.gem_number}个宝石没收 ")

    elif Game.portal.Portal_rect.colliderect(Game.player1.rect) or \
            Game.portal.Portal_rect.colliderect(Game.player2.rect) or \
            Game.portal.Portal_rect.colliderect(Game.player3.rect) or \
            Game.portal.Portal_rect.colliderect(Game.player4.rect):

        font3 = pygame.font.Font("Font/font.ttf", 50)
        score_text = font3.render(f" 英雄们没齐，还有{Game.gem_number}个宝石没收 ", True, (8, 25, 15), (255, 85, 0))
        Game.window.blit(score_text, (600, 820))
        print(f" 还有{Game.gem_number}个宝石没收 ")


if __name__ == '__main__':
    Game().__init__()
    game_star()
